package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.ICourseService;
import com.ruoyi.system.service.IScoreService;
import com.ruoyi.system.service.IStudentService;
import com.ruoyi.system.service.ITeacherService;
import jdk.jfr.events.SocketReadEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author sett
 * @date 2023年06月25日 21:24
 * @title
 */
@RestController
@RequestMapping("/system/charts")
public class FrontEndController {


    //成绩占比图
    @Autowired
    private IScoreService scoreService;

    @Autowired
    private ITeacherService teacherService;

    @Autowired
    private IStudentService studentService;

    @Autowired
    private ICourseService courseService;

    @GetMapping("/getRoseData")
    public AjaxResult getRoseData(){
        List<Score> scores = scoreService.selectScoreList(new Score());
        List<Data> resultList = new ArrayList<>();

        int flag1 = 1;
        int flag2 = 2   ;
        int flag3 = 0;
        int flag4 = 0;
        int flag5 = 0;

        for (Score score : scores) {
            Long s = score.getScore();
            if (s < 60) {
                flag1++;
            } else if (s < 70) {
                flag2++;
            } else if (s < 80) {
                flag3++;
            } else if (s < 90) {
                flag4++;
            } else {
                flag5++;
            }
        }

        Data data1 = new Data();
        data1.setName("不及格");
        data1.setValue(flag1);
        resultList.add(data1);

        Data data2 = new Data();
        data2.setName("及格");
        data2.setValue(flag2);
        resultList.add(data2);

        Data data3 = new Data();
        data3.setName("中等");
        data3.setValue(flag3);
        resultList.add(data3);

        Data data4 = new Data();
        data4.setName("良好");
        data4.setValue(flag4);
        resultList.add(data4);

        Data data5 = new Data();
        data5.setName("优秀");
        data5.setValue(flag5);
        resultList.add(data5);

        return AjaxResult.success(resultList);
    }

    @GetMapping("/getListData")
    public AjaxResult getListData(){
        List<Score> scores = scoreService.selectScoreList(new Score());
        List<List<String>> list = new ArrayList<>();
        for(Score score:scores){
            List<String> li = new ArrayList<>();
            li.add(studentService.selectStudentById(score.getStudentId()).getName());
            li.add(String.valueOf(score.getScore()));
            li.add(courseService.selectCourseById(score.getCourseId()).getName());
            list.add(li);

        }
        return AjaxResult.success(list);
    }


    @GetMapping("/getRankList")
    public AjaxResult getRankList(){
        List<Score> scores = scoreService.selectScoreList(new Score());
        int len = Math.min(scores.size(), 10);
        Collections.sort(scores, Comparator.comparing(Score::getScore).reversed());
        List<Data> list=  new ArrayList<>();
        for(int i =0;i<len;i++){
            Data data = new Data();
            data.setName(studentService.selectStudentById(scores.get(i).getStudentId()).getName());
            data.setValue(Math.toIntExact(scores.get(i).getScore()));
            list.add(data);
        }
        return AjaxResult.success(list);
    }


    /*
     * @date 2023/6/26 11:55
     *
     *
     *    {
          val: 32, //数据
          blat: [86.9023, 41.148], //发射点
          elon: [87.8695, 31.6846], //接收点
          bcitysim: "新疆", //发射省的名字
          ecitysim: "西藏", //接收省的名字
        },
     */

    @GetMapping("/getMapData")
    public AjaxResult getMapData(){
        List<Student> students = studentService.selectStudentList(new Student());
        Map<String, Integer> provinceStudentCount = new HashMap<>();
        for (Student student : students) {
            String province = student.getProvince();
            Integer count = provinceStudentCount.getOrDefault(province, 0);
            provinceStudentCount.put(province, count + 1);
        };
        List<List<String>> list=  new ArrayList<>();
        for(String s : provinceStudentCount.keySet()){
            List<String> li = new ArrayList<>();
            li.add(String.valueOf(provinceStudentCount.get(s)));
            li.add(s);
            li.add("浙江");
            list.add(li);
        }
        return AjaxResult.success(list);
    }

    @GetMapping("/getAverageScorebyTeacher")
    public AjaxResult getAverageScorebyTeacher(){
        List<Score> scores = scoreService.selectScoreList(new Score());

        Map<Long, List<Long>> teacherScoresMap = new HashMap<>();
// 遍历所有成绩，将同一老师的成绩放在同一个列表中
        for (Score score : scores) {
            Long teacherName = score.getTeacherId();
            Long scoreValue = score.getScore();

            List<Long> teacherScores = teacherScoresMap.getOrDefault(teacherName, new ArrayList<>());
            teacherScores.add(scoreValue);
            teacherScoresMap.put(teacherName, teacherScores);
        }

        List<Data> list = new ArrayList<>();
// 遍历同一老师的成绩列表，计算平均分并存入新的Map中
        for (Map.Entry<Long, List<Long>> entry : teacherScoresMap.entrySet()) {
            Long teacherName = entry.getKey();
            List<Long> teacherScores = entry.getValue();

            double totalScore = 0.0;
            for (Long score : teacherScores) {
                totalScore += score;
            }
            int averageScore = (int) Math.round(totalScore / teacherScores.size());
            Data data = new Data();
            data.setName(teacherService.selectTeacherById(teacherName).getName());
            data.setValue(averageScore);
            list.add(data);
        }
        return AjaxResult.success(list);
    }

    @GetMapping("/getCourseInfo")
    public AjaxResult getCourseInfo(){
        List<List<String>> list = new ArrayList<>();
        List<Teacher> teachers = teacherService.selectTeacherList(new Teacher());
        for(Teacher teacher:teachers){
            List<String> li = new ArrayList<>();
            li.add(teacher.getName());
            li.add(courseService.selectCourseById(teacher.getCourseId()).getName());
            list.add(li);
        }
        return AjaxResult.success(list);
    }

    @GetMapping("/getTop10")
    public AjaxResult getTop10(){
        List<Score> scores = scoreService.selectScoreList(new Score());
// 按照分数从高到低排序
        scores.sort(Comparator.comparingDouble(Score::getScore).reversed());

// 获取前10名
        List<Score> top10 = scores.stream().limit(10).collect(Collectors.toList());
        List<Data> list = new ArrayList<>();
        for(Score score:top10){
            Data data = new Data();
            data.setValue(Math.toIntExact(score.getScore()));
            data.setName(studentService.selectStudentById(score.getStudentId()).getName());
            list.add(data);
        }
        return AjaxResult.success(list);
    }

    @GetMapping("/getScoreRecentChange")
    public AjaxResult getScoreRecent(){
        List<Score> scores = scoreService.selectScoreList(new Score());
        int size = scores.size();
        List<Score> last10Scores = scores.subList(Math.max(size - 10, 0), size);
        List<List<String>> list = new ArrayList<>();
        List<String> names = new ArrayList<>();
        List<String> values = new ArrayList<>();
        for(Score score:last10Scores){
            names.add(studentService.selectStudentById(score.getStudentId()).getName());
            values.add(String.valueOf(score.getScore()));
        }
        list.add(names);
        list.add(values);
        return AjaxResult.success(list);
    }

}
